home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / VSPrintf.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  4KB  |  215 lines

  1. /*
  2. **    VSPrintf.c
  3. **
  4. **    Formatted output to memory
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* StuffChar(REG(a3) struct FormatContext *Context,REG(d0) UBYTE Char):
  17.      *
  18.      *    Stuffs a character into the buffer, unless there is not
  19.      *    enough room left.
  20.      */
  21.  
  22. VOID ASM
  23. StuffChar(REG(a3) struct FormatContext *Context,REG(d0) UBYTE Char)
  24. {
  25.     if(Context->Size > 0)
  26.     {
  27.         *Context->Index++ = Char;
  28.  
  29.         if(--Context->Size == 1)
  30.         {
  31.             *Context->Index = 0;
  32.             Context->Size = 0;
  33.         }
  34.     }
  35. }
  36.  
  37.     /* LimitedVSPrintf(LONG Size,STRPTR Buffer,STRPTR FormatString,...):
  38.      *
  39.      *    Like VSPrintf, but won't write more than the given number
  40.      *    of bytes.
  41.      */
  42.  
  43. VOID
  44. LimitedVSPrintf(LONG Size,STRPTR Buffer,STRPTR FormatString,va_list VarArgs)
  45. {
  46.     struct FormatContext Context;
  47.  
  48.     Context.Index    = Buffer;
  49.     Context.Size    = Size;
  50.  
  51. #ifdef USE_GLUE
  52.     RawDoFmt(FormatString,(APTR)VarArgs,(PUTCHAR)StuffCharGlue,(APTR)&Context);
  53. #else
  54.     RawDoFmt(FormatString,(APTR)VarArgs,(PUTCHAR)StuffChar,(APTR)&Context);
  55. #endif    /* USE_GLUE */
  56. }
  57.  
  58.     /* LimitedSPrintf(LONG Size,STRPTR Buffer,STRPTR FormatString,...):
  59.      *
  60.      *    Like SPrintf, but won't write more than the given number
  61.      *    of bytes.
  62.      */
  63.  
  64. VOID
  65. LimitedSPrintf(LONG Size,STRPTR Buffer,STRPTR FormatString,...)
  66. {
  67.     va_list VarArgs;
  68.  
  69.     va_start(VarArgs,FormatString);
  70.     LimitedVSPrintf(Size,Buffer,FormatString,VarArgs);
  71.     va_end(VarArgs);
  72. }
  73.  
  74.     /* VSPrintf(STRPTR Buffer,STRPTR FormatString,va_list VarArgs):
  75.      *
  76.      *    Just like vsprintf(), but using the ROM routines.
  77.      */
  78.  
  79. VOID
  80. VSPrintf(STRPTR Buffer,STRPTR FormatString,va_list VarArgs)
  81. {
  82.     RawDoFmt(FormatString,(APTR)VarArgs,(PUTCHAR)"\x16\xC0\x4E\x75",(APTR)Buffer);
  83. }
  84.  
  85.     /* SPrintf(STRPTR Buffer,STRPTR FormatString,...):
  86.      *
  87.      *    Just like sprintf(), but using the ROM routines.
  88.      */
  89.  
  90. VOID
  91. SPrintf(STRPTR Buffer,STRPTR FormatString,...)
  92. {
  93.     va_list VarArgs;
  94.  
  95.     va_start(VarArgs,FormatString);
  96.     VSPrintf(Buffer,FormatString,VarArgs);
  97.     va_end(VarArgs);
  98. }
  99.  
  100.     /* CountChar(REG(a3) ULONG *Count):
  101.      *
  102.      *    Counts the number of bytes to stuff into the output buffer.
  103.      */
  104.  
  105. VOID ASM
  106. CountChar(REG(a3) ULONG *Count)
  107. {
  108.     *Count = *Count + 1;
  109. }
  110.  
  111.     /* GetFormatLength(STRPTR FormatString,va_list VarArgs):
  112.      *
  113.      *    Before calling VSPrintf or the like, count the number of
  114.      *    characters that would go into the format buffer (including
  115.      *    the trailing NUL character).
  116.      */
  117.  
  118. ULONG
  119. GetFormatLength(STRPTR FormatString,va_list VarArgs)
  120. {
  121.     ULONG Count;
  122.  
  123.     Count = 0;
  124.  
  125. #ifdef USE_GLUE
  126.     RawDoFmt(FormatString,(APTR)VarArgs,(PUTCHAR)CountCharGlue,(APTR)&Count);
  127. #else
  128.     RawDoFmt(FormatString,(APTR)VarArgs,(PUTCHAR)CountChar,(APTR)&Count);
  129. #endif    /* USE_GLUE */
  130.  
  131.     return(Count);
  132. }
  133.  
  134.     /* GetFormatLengthArgs(STRPTR FormatString,...):
  135.      *
  136.      *    VarArgs version of GetFormatLength.
  137.      */
  138.  
  139. ULONG
  140. GetFormatLengthArgs(STRPTR FormatString,...)
  141. {
  142.     va_list VarArgs;
  143.     ULONG Count;
  144.  
  145.     va_start(VarArgs,FormatString);
  146.     Count = GetFormatLength(FormatString,VarArgs);
  147.     va_end(VarArgs);
  148.  
  149.     return(Count);
  150. }
  151.  
  152.     /* LimitedStrcat(LONG Size,STRPTR To,STRPTR From):
  153.      *
  154.      *    Like strcat, but limited by the size of the
  155.      *    destination buffer.
  156.      */
  157.  
  158. VOID
  159. LimitedStrcat(LONG Size,STRPTR To,STRPTR From)
  160. {
  161.     while(*To)
  162.     {
  163.         To++;
  164.         Size--;
  165.     }
  166.  
  167.     while(*From && Size > 0)
  168.     {
  169.         *To++ = *From++;
  170.  
  171.         Size--;
  172.     }
  173.  
  174.     *To = 0;
  175. }
  176.  
  177.     /* LimitedStrcpy(LONG Size,STRPTR To,STRPTR From):
  178.      *
  179.      *    Like strcpy, but limited by the size of the
  180.      *    destination buffer.
  181.      */
  182.  
  183. VOID
  184. LimitedStrcpy(LONG Size,STRPTR To,STRPTR From)
  185. {
  186.     while(*From && Size > 0)
  187.     {
  188.         *To++ = *From++;
  189.  
  190.         Size--;
  191.     }
  192.  
  193.     *To = 0;
  194. }
  195.  
  196.     /* LimitedStrncpy(LONG Size,STRPTR To,STRPTR From):
  197.      *
  198.      *    Like strncpy, but limited by the size of the
  199.      *    destination buffer.
  200.      */
  201.  
  202. VOID
  203. LimitedStrncpy(LONG Size,STRPTR To,STRPTR From,LONG FromSize)
  204. {
  205.     while(*From && Size > 0 && FromSize > 0)
  206.     {
  207.         *To++ = *From++;
  208.  
  209.         Size--;
  210.         FromSize--;
  211.     }
  212.  
  213.     *To = 0;
  214. }
  215.